<template>
  <div>
    <h1>Didi</h1>
    {{ count }}
    <button @click="byebye">拜拜</button>
  </div>
</template>

<script>
import PubSub from "pubsub-js";
export default {
  name: "Didi",
  data() {
    return {
      count: 0,
      pubId: null,
    };
  },
  mounted() {
    this.pubId = PubSub.subscribe("count", (_, count) => {
      // 第一个参数是事件名称，第二个参数是传递的值
      // console.log(a,count);
      this.count = count;
    });
  },
  methods: {
    byebye() {
      // // 会取消全部的订阅
      // PubSub.unsubscribe('count')

      //接收订阅id，是订阅方法的返回值，取消当前组件的当前订阅
      PubSub.unsubscribe(this.pubId);
    },
  },
};
</script>

<style>
</style>